﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using lt_pmbc_001.DTO;
using lt_pmbc_001.DAL;
using lt_pmbc_001.Helpers;

namespace lt_pmbc_001.Forms
{
    public partial class FrmThemDiaDiem : Office2007Form
    {
        //  event handler to transfer data to other forms
        public EventHandler Handler { get; set; }
        private int ID;

        public FrmThemDiaDiem(int id = -1)
        {
            InitializeComponent();
            this.ID = id;
            if (id != -1)
            {
                var objBUS = new DiaDiemDAL(GlobalVariables.g_strPath);
                var ds = objBUS.SelectByID(id);
                var row = ds.Tables[0].Rows[0];
                txtID.Text = row["ID"].ToString();
                txtDiaDiem.Text = row["DiaDiem"].ToString();
                txtDiaChi.Text = row["DiaChi"].ToString();
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            var errorText = "";
            if (!ValidateInput(ref errorText))
            {
                MessageBoxEx.Show(errorText, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            var result = false;

            // Update
            if (this.ID != -1)
            {
                result = ActionUpdate();
            }
            else //Insert 
            {
                result = ActionAdd();
            }

            if (result)
            {
                MessageBoxEx.Show("Lưu dữ liệu thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBoxEx.Show("Lưu dữ liệu thất bại", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void FrmThemDiaDiem_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Handler(sender, e);
        }

        private bool ValidateInput(ref string errorText)
        {
            if (string.Empty.Equals(txtDiaDiem.Text))
            {
                errorText = "Vui lòng nhập địa điểm";
                return false;
            }
            if (string.Empty.Equals(txtDiaChi.Text))
            {
                errorText = "Vui lòng nhập địa chỉ";
                return false;
            }
            return true;
        }

        private bool ActionAdd()
        {
            try
            {
                var item = new DiaDiemDTO
                {
                    DiaDiem = txtDiaDiem.Text,
                    DiaChi = txtDiaChi.Text
                };

                var objBUS = new DiaDiemDAL(GlobalVariables.g_strPath);
                if (!objBUS.Insert(item))
                {
                    return false;
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        private bool ActionUpdate()
        {
            try
            {
                var item = new DiaDiemDTO
                {
                    ID = int.Parse(txtID.Text),
                    DiaDiem = txtDiaDiem.Text,
                    DiaChi = txtDiaChi.Text
                };
                var objBUS = new DiaDiemDAL(GlobalVariables.g_strPath);
                return objBUS.Update(item);
            }
            catch
            {
                return false;
            }
        }
    }
}
